/*
 * @fileOverview reportgear核心函数
 * @author <a href="mailto:lexloo@126.com">lexloo</a>
 */

/**
 * gear.js 核心函数
 */
(function() {
	var proto = 'prototype', 
		hasOwn = Object.prototype.hasOwnProperty,
		doc = document, 
		win = window, 
		appendChild = 'appendChild', 
		apply =	'apply', 
		call = 'call', 
		split = 'split', 
		plugInLoaded = '$loaded$';

	/**
	 * 全局对象
	 */
	Global = (function() {
		return this;
	})();

	Gear = {};
	Gear.kernel = {};
	Gear.kernel.version = '1.0.0';
	Gear.kernel._plugIns = {};
	Gear.kernel.jsBaseURL = '';
	Gear.kernel.cssBaseURL = '';

	/**
	 * 部署后地址动态获取，本地运行时请外部改写该值
	 */
	Gear.kernel.baseURL = (function() {
		// var url = doc.location.pathname;
			// var index = url.substr(1).indexOf("/");
			// url = url.substr(0, index + 1);
			//
			// return url;
			return '';
		})();

	/**
	 * 加载插件
	 * 
	 * @param names 插件名
	 */
	Gear.kernel.loadPlugIn = function(names) {
		var nameArr = names[split](',');

		for ( var i = 0, len = nameArr.length; i < len; i++) {
			var plugIn = Gear.kernel._getPlugIn(nameArr[i]);
			if (!plugIn[plugInLoaded]) {/* 内部定义个变量，标记插件是否已经加载 */
				Gear.kernel._loadJs(plugIn.url);
				plugIn[plugInLoaded] = true;
			}
		}
	};

	/**
	 * 插件定义
	 * 
	 * @param name 插件名称
	 * @param content 插件内容
	 * @param requires 需求
	 */
	Gear.kernel.DefPlugIn = function(name, content, requires) {
		var plugIn = Gear.kernel._getPlugIn(name);
		if (!plugIn[plugInLoaded]) {
			plugIn = Gear.kernel.loadPlugIn(name);
		}

		if (requires && typeof requires == 'string') {
			var requireArr = requires[split](',');
			for ( var i = 0, len = requireArr.length; i < len; i++) {
				var r = requireArr[i];

				if (/.js$/.test(r)) {
					Gear.kernel.loadPlugIn(r);
				} else if (/.css$/.test(r)) {
					Gear.kernel._loadCss(r);
				}
			}
		}

		if (content) {
			content();
		}
	};

	/**
	 * 获取插件
	 * 
	 * @param name 插件插件名称
	 */
	Gear.kernel._getPlugIn = function(name) {
		var plugIn = Gear.kernel._plugIns[name];

		if (!plugIn) {
			/* 当前支持相对路径 */
			plugIn = {
				'name' : name, plugInLoaded : false, 'url' : name
			}
		}

		return plugIn;
	};

	/**
	 * 加载JS库文件
	 * 
	 * @param {String} libName 库文件名
	 */
	Gear.kernel._loadJs = function(jsName) {
		var scripts = doc.getElementsByTagName("script");
		for ( var k = 0, len = scripts.length; k < len; k++) {
			if (scripts[k].src.indexOf(jsName) != -1) {
				return;
			}
		}

		var script = doc.createElement("script");
		var src = Gear.kernel.baseURL + Gear.kernel.jsBaseURL + jsName;
		script.setAttribute('src', src);
		script.setAttribute('type', 'text/javascript');

		var toDoc = doc.getElementsByTagName("head").length ? doc.getElementsByTagName("head")[0] : doc.body;
		toDoc[appendChild](script);
	};

	/**
	 * 加载CSS样式表
	 * 
	 * @param {String} cssName 样式表名
	 */
	Gear.kernel._loadCss = function(cssName) {
		var csss = doc.getElementsByTagName("link");
		for ( var k = 0, len = csss.length; k < len; k++) {
			if (csss[k].href.indexOf(cssName) != -1) {
				return;
			}
		}

		var css = doc.createElement('link');
		css.setAttribute('type', 'text/css');
		css.setAttribute('rel', 'stylesheet');
		var href = Gear.kernel.baseURL + Gear.kernel.cssBaseURL + cssName;
		css.setAttribute('href', href);

		var toDoc = doc.getElementsByTagName("head").length ? doc.getElementsByTagName("head")[0] : doc.body;
		toDoc[appendChild](css);
	};

	/* 加载核心插件 */
	Gear.kernel.loadPlugIn('core/html5.js');
	Gear.kernel.loadPlugIn('core/lang.js');
	Gear.kernel.loadPlugIn('core/json.js');
	Gear.kernel.loadPlugIn('plugIns/graph/core.js');
})();